class DB {
	constructor(dbname, tableName) {
		this.dbname = dbname
		this.tableName = tableName
		this.version = 1
		this.dbdesc = 'goods storage'
		this.dbsize = 200 * 1024 * 1024
		this.dataBase = 'bncf'
		return this
	}
	/**
	 * 打开数据库
	 * @returns  dataBase:打开成功   null:打开失败
	 */
	websqlOpenDB() {
		/*数据库有就打开 没有就创建*/
		try {
			let dbBase = null;
			console.log('--websqlOpenDB--' + this.dbname, this.version, this.dbdesc, this.dbsize);
			dbBase = openDatabase(this.dbname, this.version, this.dbdesc, this.dbsize, function() {});
			if (dbBase) {
				console.log("数据库创建/打开成功!");
			} else {
				console.log("数据库创建/打开失败！");
			}
			this.dataBase = dbBase
			console.log("dataBase==="+this.dataBase)
			return this.dataBase;
		} catch (e) {
			console.error("open db fail==" + e.message)
		}
	}

	/**
	 * 新建数据库里面的表单
	 * @param tableName:表单名
	 */
	websqlCreatTable() {
		try {
			//  chinaAreaOpenDB();
			let creatTableSQL = 'CREATE TABLE IF  NOT EXISTS ' + this.tableName +
				' (rowid INTEGER PRIMARY KEY AUTOINCREMENT, goods text,datestamp int,pageno int)';
			let res = true;
			this.dataBase.transaction(function(ctx, result) {
				ctx.executeSql(creatTableSQL, [], function(ctx, result) {
					res = true;
					console.log("表创建成功 " + this.tableName);
				}, function(tx, error) {
					res = false;
					console.log('创建表失败:' + this.tableName + error.message);
				});
			});
			return res;
		} catch (e) {
			console.error("create table fail ==" + e.message)
		}
	}

	/**
	 * 往表单里面插入数据
	 */
	websqlInsterDataToTable(goods, datestamp, pageNo) {
		let insterTableSQL = 'INSERT INTO ' + this.tableName + ' (goods,storagestamp,pageNo) VALUES (?,?,?)';
		let res = false;
		this.dataBase.transaction(function(ctx) {
			ctx.executeSql(insterTableSQL, [goods, datestamp, pageNo], function(ctx, result) {
					res = true;
					console.log("插入成功");
				},
				function(tx, error) {
					res = false;
					console.log('插入失败: ' + error.message);
				});
		});
		return res;
	}
	/**
	 * 获取数据库一个表单里面的所有数据
	 * @param tableName:表单名
	 * 返回数据集合
	 */
	websqlGetAllData() {
		let selectALLSQL = 'SELECT * FROM ' + this.tableName;
		this.dataBase.transaction(function(ctx) {
			ctx.executeSql(selectALLSQL, [], function(ctx, result) {
					console.log('查询成功: ' + this.tableName + result.rows.length);
					let len = result.rows.length;
					for (let i = 0; i < len; i++) {
						console.log("NAME = " + result.rows.item(i).NAME);
						console.log("AGE = " + result.rows.item(i).AGE);
						console.log("HEIGHT = " + result.rows.item(i).HEIGHT);
						console.log("WEIGTH = " + result.rows.item(i).WEIGTH);
						console.log("-------- 我是分割线 -------");
					}
				},
				function(tx, error) {
					console.log('查询失败: ' + error.message);
				});
		});
	}
	/**
	 * 删除表单里的全部数据
	 * @param tableName:表单名
	 */
	websqlDeleteAllDataFromTable(nowstamp) {
		let deleteTableSQL = 'DELETE FROM ' + this.tableName + 'WHERE storagestamp -' + parseInt(nowstamp) < (1000 * 60 * 24 *
			24);
		// localStorage.removeItem(this.tableName);
		this.dataBase.transaction(function(ctx, result) {
			ctx.executeSql(deleteTableSQL, [], function(ctx, result) {
				console.log("删除表成功 " + this.tableName);
			}, function(tx, error) {
				console.log('删除表失败:' + this.tableName + error.message);
			});
		});
	}
	websqlGenDataByDatestamp(pageNo) {
		let selectSQL = 'SELECT * FROM ' + this.tableName + 'where pageNo=' + pageNo;
		let res = [];
		this.dataBase.transaction(function(ctx) {
			ctx.executeSql(selectALLSQL, [], function(ctx, result) {
					console.log('查询成功: ' + this.tableName + result.rows.length);
					res = result.rows
				},
				function(tx, error) {
					res = []
					console.log('查询失败: ' + error.message);
				});
		});
		return res;
	}
}

export default DB
